package cn.achao.csr.api.model.entity;

import cn.achao.csr.api.SequenceApi;
import cn.achao.csr.base.constant.SecurityConstants;
import cn.achao.csr.base.entity.BaseEntity;
import cn.achao.csr.base.utils.SpringUtils;
import com.baomidou.mybatisplus.annotation.*;

import java.io.Serializable;
import java.time.LocalDateTime;
import java.util.List;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

/**
 * 用户表
 * @TableName sys_user
 */
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
@TableName(value = "sys_user")
@JsonIgnoreProperties({"userExpand"})
public class SysUser extends BaseEntity implements Serializable {
    /**
     * 主键
     */
    @TableId(type = IdType.ASSIGN_ID)
    private Long id = super.getId();

    /**
     * 账号
     */
    private String userAccount;

    /**
     * 昵称
     */
    private String nickName;

    /**
     * 密码
     */
    private String userPwd;

    /**
     * 手机号码
     */
    private String accNbr;

    /**
     * 邮箱
     */
    private String email;

    /**
     * 头像地址
     */
    private String avatarUrl;

    /**
     * 密码盐
     */
    private String passwordSalt;

    /**
     * 拓展id
     */
    private Long expandId;

    /**
     * 用户状态
     */
    private String status;

    /**
     * 创建者
     */
    private String createBy;

    /**
     * 更新者
     */
    private String updateBy;

    /**
     * 创建时间
     */
    private LocalDateTime createTime;

    /**
     * 更新时间
     */
    private LocalDateTime updateTime;

    /**
     * 是否删除
     */
    @TableLogic(value = "0", delval = "1")
    private Integer isDel;

    /**
     * 备注
     */
    private String remark;

    /**
     * 用户拓展信息
     */
    @TableField(exist = false)
    private SysUserExpand userExpand;

    /**
     * 部门ID
     */
    private Long deptId;
    /**
     * 部门
     */
    private SysDept dept;
    /**
     * 角色列表
     */
    @TableField(exist = false)
    private List<SysRole> roles;

    /** 角色组 */
    @TableField(exist = false)
    private Long[] roleIds;

    /** 角色ID */
    @TableField(exist = false)
    private Long roleId;

    @TableField(exist = false)
    private static final long serialVersionUID = 1L;

    /**
     * INSERT INTO `csr`.`leaf_alloc` (`biz_tag`, `max_id`, `step`, `description`)
     * VALUES ('seq_sys_user', 1, 100, 'seq_sys_user主键');
     * @return
     */
    @Override
    public String sequenceCode() {
        return "seq_sys_user";
    }

    public SysUser(Boolean generateId) {
        super(generateId);
    }

    /**
     * 该用户是否为管理员
     * @return
     */
    public boolean isAdmin() {
        return isAdmin(this.id);
    }

    public static boolean isAdmin(Long userId) {
        return userId != null && 1L == userId;
    }
}